package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.AuctionServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.delegator.ItemsWatchListServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AuctionBids;
import com.printeverywhere.dto.AuctionWonlist;
import com.printeverywhere.dto.BidStatus;
import com.printeverywhere.dto.ItemNote;
import com.printeverywhere.dto.ItemOffers;
import com.printeverywhere.dto.ItemWantedBestOffer;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.ItemsWatchlist;
import com.printeverywhere.dto.ListingOptions;
import com.printeverywhere.dto.MyAccountCount;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.User;
import com.printeverywhere.dto.UserFavoritesUsers;
import com.printeverywhere.dto.UserRating;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;
import com.printeverywhere.util.SessionUtil;
import com.printeverywhere.util.Sort;

public class MyAccountTenderAction extends FrontBaseSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private User user;

	private ListingOptions listingOptions;

	private List<Items> itemsListTemp = new ArrayList<Items>();

	private List<Items> itemsList = new ArrayList<Items>();

	private SearchCriteria searchCriteria = new SearchCriteria();

	private List<ItemsWatchlist> itemsWatchList = new ArrayList<ItemsWatchlist>();

	private SearchListCriteria searchListCriteria = new SearchListCriteria();

	private List<ItemOffers> itemOffersTemp = new ArrayList<ItemOffers>();

	private List<ItemOffers> itemOffers = new ArrayList<ItemOffers>();

	private Items itemAddTOFavorite;

	private Integer askSellerId = 0;

	private UserFavoritesUsers favoritesUsers = new UserFavoritesUsers();

	private List<AuctionWonlist> wonlist = new ArrayList<AuctionWonlist>();

	private List<Transactions> transactionsList;

	private Map<Integer, Transactions> transactionsMap = new HashMap<Integer, Transactions>();

	private Date invoiceDate = DateUtil.getTodaysDate();

	private String action;

	private String itemTypeFrom;

	private String redirectAction;

	public String getItemTypeFrom() {
		return itemTypeFrom;
	}

	public void setItemTypeFrom(String itemTypeFrom) {
		this.itemTypeFrom = itemTypeFrom;
	}

	public Date getInvoiceDate() {
		return invoiceDate;
	}

	public void setInvoiceDate(Date invoiceDate) {
		this.invoiceDate = invoiceDate;
	}

	public List<Transactions> getTransactionsList() {
		return transactionsList;
	}

	public void setTransactionsList(List<Transactions> transactionsList) {
		this.transactionsList = transactionsList;
	}

	public UserFavoritesUsers getFavoritesUsers() {
		return favoritesUsers;
	}

	public void setFavoritesUsers(UserFavoritesUsers favoritesUsers) {
		this.favoritesUsers = favoritesUsers;
	}

	public Items getItemAddTOFavorite() {
		return itemAddTOFavorite;
	}

	public void setItemAddTOFavorite(Items itemAddTOFavorite) {
		this.itemAddTOFavorite = itemAddTOFavorite;
	}

	public Integer getAskSellerId() {
		return askSellerId;
	}

	public void setAskSellerId(Integer askSellerId) {
		this.askSellerId = askSellerId;
	}

	public List<ItemOffers> getItemOffers() {
		return itemOffers;
	}

	public void setItemOffers(List<ItemOffers> itemOffers) {
		this.itemOffers = itemOffers;
	}

	public ListingOptions getListingOptions() {
		return listingOptions;
	}

	public void setListingOptions(ListingOptions listingOptions) {
		this.listingOptions = listingOptions;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public MyAccountTenderAction() {
		super(true, false);
		setMyAccount(true);
		itemTypeFrom = "Tender";
		redirectAction="deleteTender";
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void getMyItemWanted() throws Exception{
		try{
			if((request.getParameter("action") != null) && (!request.getParameter("action").equals(AdminConstants.NONE))){
				action = request.getParameter("action");
			}
			if((request.getParameter("userId") != null) && (!request.getParameter("userId").equals(AdminConstants.NONE))){
				user = UserServiceDelegator.getUserByUserId(request.getParameter("userId"));
			}else{
				user = SessionUtil.getUser(request);
			}

			listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
			ItemWantedBestOffer itemWantedBestOffer = new ItemWantedBestOffer();
			UserRating rating;
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AdminConstants.USERID , user);
			searchCriteria.setAttribute(AttributeConstants.ITEMTYPE_ID, 7);
			searchCriteria.setAttribute("status.id", 4);
			itemsListTemp = ItemsServiceDelegator.getList(searchCriteria);
			for (Iterator<Items> iterator = itemsListTemp.iterator(); iterator.hasNext();) {
				Items type = (Items) iterator.next();
				AuctionBids auctionBids = new AuctionBids();
				String[] giftIcon = null;
				if(giftIcon != null && giftIcon.length >0){
					giftIcon = type.getGiftIcon().split(AdminConstants.SEMICOLON);
					type.setGiftIcon(giftIcon[0]);
				}
				ItemNote itemNote = new ItemNote();
				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute(AttributeConstants.ITEMS, new Object[]{type, SearchListCriteria.EQ});
				searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
				itemsWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				type.setNoOfWatches(itemsWatchList.size());
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute(AttributeConstants.ITEMS, type);
				searchCriteria.setAttribute(AttributeConstants.USER, user);
				itemNote = (ItemNote) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class).get(AdminConstants.ZERO_NUMBER) : new ItemNote());
				type.setItemNote(itemNote);
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("itemId", type);
				itemWantedBestOffer = (ItemWantedBestOffer) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class).get(AdminConstants.ZERO_NUMBER) : null);
				type.setItemWantedBestOffer(itemWantedBestOffer);
				if(itemWantedBestOffer != null){
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("ratedUser.id", itemWantedBestOffer.getUserId().getId());
					List<UserRating> ratingList = (List<UserRating>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserRating.class);
					rating = ratingList.size() > 0 ? ratingList.get(0) : null;
					if(rating != null){
						type.setUserRating(rating);
					}
				}
				if(type.getEndTime() != null && type.getEndTime().before(DateUtil.getTodaysDate())){
					type.setClosed(true);
				}else{
					type.setClosed(false);
				}
				itemsList.add(type);
			}
		}catch (Exception e) {
			addActionError("Error in my wanted.");
		}
	}

	public String getMyWanted() throws Exception{
		try{
			getMyItemWanted();

			if(request.getParameter("sort") != null && request.getParameter("sort").equalsIgnoreCase("title")){
				itemsList = Sort.byItemTitle(itemsList);
			}
			if(request.getParameter("sort") != null && request.getParameter("sort").equalsIgnoreCase("time")){
				itemsList = Sort.byItemTime(itemsList);
			}

		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while getting list for Item wanted");
			if(action != null && !action.equals(AdminConstants.NONE)){
				return "favoriteUsers";
			}else{
				return INPUT;
			}
		}

		return SUCCESS;
	}

	public String deleteItemWanred() throws Exception{
		try{
			if (request.getParameter("check_list") != null) {
				String[] checkedId = request.getParameterValues("check_list");
				if(request.getParameter("deleteMsg") != null && request.getParameter("deleteMsg").equalsIgnoreCase("delete")){
					for (String chkid : checkedId) {
						Items item = ItemsServiceDelegator.getItemsById(Integer.parseInt(chkid));
						if(item!=null){
							CommonFunctionDelegator.GeneralDelete(item);
							MyAccountCount myAccountCount = ((MyAccountCount) request.getSession().getAttribute(AttributeConstants.MYACCOUNTCOUNT));
							if(myAccountCount.getTenderMyWanteds() > 0){
								myAccountCount.setTenderMyWanteds(myAccountCount.getTenderMyWanteds() - 1);
							}
							request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);
						}
					}
					addActionMessage("My tender delete successfully");
				}
			}else{
				addActionError("Select Item for delete");
				getMyItemWanted();
				return INPUT;
			}
		}catch (Exception e) {
			e.printStackTrace();
			getMyItemWanted();
			addActionError("Error While deleting My tender");
			return INPUT;
		}
		getMyItemWanted();
		return SUCCESS;
	}

	public String cancelMyWanted() throws Exception{
		try{
			if (request.getParameter("cancelId") != null) {
				String itemId = request.getParameter("cancelId");
					Items item = ItemsServiceDelegator.getItemsById(Integer.parseInt(itemId));
					BidStatus bidStatus = (BidStatus) CommonFunctionDelegator.getObjectById("id", 1, BidStatus.class);
					if(item != null){
						item.setStatus(bidStatus);
						CommonFunctionDelegator.generalUpdatebyobj(item);
						MyAccountCount myAccountCount = ((MyAccountCount) request.getSession().getAttribute(AttributeConstants.MYACCOUNTCOUNT));
						if(myAccountCount.getTenderMyWanteds() > 0){
							myAccountCount.setTenderMyWanteds(myAccountCount.getTenderMyWanteds() - 1);
						}
						request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);
					}
					addActionMessage("My Tender Cancel successfully");
				}else{
					addActionError("Error While retrive Item");
					getMyItemWanted();
					return INPUT;
			}
		}catch (Exception e) {
			e.printStackTrace();
			getMyItemWanted();
			addActionError("Error While cancel My Tender");
			return INPUT;
		}
		getMyItemWanted();
		return SUCCESS;
	}

	public String watchOther() throws Exception{
		user = SessionUtil.getUser(request);
		UserRating rating;
		UserFavoritesUsers userFavoritesUsers = new UserFavoritesUsers();
		listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		ItemWantedBestOffer itemWantedBestOffer = new ItemWantedBestOffer();
		searchListCriteria.removeAllAttribute();
		searchListCriteria.setAttribute("user.id", new Object[]{user.getId(), SearchListCriteria.EQ});
		List<ItemsWatchlist> watchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
		if(watchList != null){
			for(int i=0;i<watchList.size();i++){
				if(watchList.get(i).getItems().getItemtype().getId() == 7){
					Items items = ItemsServiceDelegator.getItemsById(watchList.get(i).getItems().getItemId());
					if(items.getStatus().getId() == 4){
						searchCriteria.removeAllAttribute();
						searchCriteria.setAttribute("itemId.itemId", items.getItemId());
						itemWantedBestOffer = (ItemWantedBestOffer) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class).get(AdminConstants.ZERO_NUMBER) : null);
						items.setItemWantedBestOffer(itemWantedBestOffer);
						if(itemWantedBestOffer != null){
							searchCriteria.removeAllAttribute();
							searchCriteria.setAttribute("ratedUser.id", itemWantedBestOffer.getUserId().getId());
							List<UserRating> ratingList = (List<UserRating>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserRating.class);
							rating = ratingList.size() > 0 ? ratingList.get(0) : null;
							if(rating != null){
								items.setUserRating(rating);
							}
						}
						searchCriteria.removeAllAttribute();
						searchCriteria.setAttribute("ratedUser.id", items.getUserId().getId());
						List<UserRating> ratingList = (List<UserRating>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserRating.class);
						UserRating selfRating = ratingList.size() > 0 ? ratingList.get(0) : null;
						if(selfRating != null){
							items.setSelfRate(selfRating.getRating());
						}
						if(items.getEndTime().before(DateUtil.getTodaysDate())){
							items.setClosed(true);
						}else{
							items.setClosed(false);
						}
						itemsListTemp.add(items);
					}
				}
			}
		}
		ItemNote itemNote = new ItemNote();

		for (Iterator<Items> iterator = itemsListTemp.iterator(); iterator.hasNext();) {
			Items type = (Items) iterator.next();
			AuctionBids auctionBids = new AuctionBids();
			String[] giftIcon = null;
			if(giftIcon != null && giftIcon.length >0){
				giftIcon = type.getGiftIcon().split(AdminConstants.SEMICOLON);
				type.setGiftIcon(giftIcon[0]);
			}
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("user.id", user.getId());
			searchCriteria.setAttribute("userIdFav.id", type.getUserId().getId());
			List<UserFavoritesUsers> tmpList = (List<UserFavoritesUsers>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserFavoritesUsers.class);
			userFavoritesUsers = (UserFavoritesUsers) (tmpList.size() > 0 ? tmpList.get(AdminConstants.ZERO_NUMBER) : null);
			type.setFavoritesUsers(userFavoritesUsers);
			searchListCriteria.removeAllAttribute();
			searchListCriteria.setAttribute(AttributeConstants.ITEMS, new Object[]{type, SearchListCriteria.EQ});
			searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
			itemsWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
			type.setNoOfWatches(itemsWatchList.size());
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AttributeConstants.ITEMS, type);
			searchCriteria.setAttribute(AttributeConstants.USER, user);
			itemNote = (ItemNote) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class).get(AdminConstants.ZERO_NUMBER) : new ItemNote());
			type.setItemNote(itemNote);
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("itemId", type);
			itemWantedBestOffer = (ItemWantedBestOffer) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class).get(AdminConstants.ZERO_NUMBER) : null);
			type.setItemWantedBestOffer(itemWantedBestOffer);
			itemsList.add(type);
		}
		if(itemsList.size() > 0){
			if(request.getParameter("sort") != null && request.getParameter("sort").equalsIgnoreCase("title")){
				itemsList = Sort.byItemTitle(itemsList);
			}
			if(request.getParameter("sort") != null && request.getParameter("sort").equalsIgnoreCase("time")){
				itemsList = Sort.byItemTime(itemsList);
			}
			if(request.getParameter("sort") != null && request.getParameter("sort").equalsIgnoreCase("price")){
				itemsList = Sort.byItemBuyPrice(itemsList);
			}
		}
		return SUCCESS;
	}

	public String viewSimilarItem() throws Exception{
		if (request.getParameter("cid") != null) {
			user = SessionUtil.getUser(request);
			Items searchItem = ItemsServiceDelegator.getItemsById(Integer.parseInt(request.getParameter("cid")));
			searchListCriteria.removeAllAttribute();
			searchListCriteria.setAttribute("userId", new Object[]{user, SearchListCriteria.NE});
			//searchListCriteria.setAttribute("generalCategories.categoryId", new Object[]{searchItem.getGeneralCategories().getCategoryId(), SearchListCriteria.EQ});
			searchListCriteria.setAttribute("status.id", new Object[]{4, SearchListCriteria.EQ});
			itemsListTemp = ItemsServiceDelegator.getListBySearchListCriteria(searchListCriteria);
			for (Iterator<Items> iterator = itemsListTemp.iterator(); iterator.hasNext();) {
				Items type = (Items) iterator.next();
				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute(AttributeConstants.ITEMS, new Object[]{type, SearchListCriteria.EQ});
				searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
				itemsWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				type.setNoOfWatches(itemsWatchList.size());
				if(type.getEndTime().before(DateUtil.getTodaysDate())){
					type.setClosed(true);
				}else{
					type.setClosed(false);
				}
				itemsList.add(type);
			}
		}
		return SUCCESS;
	}

	public String viewSellersOtherItems() throws Exception{
		try{
			if ((request.getParameter("action") != null) && (!request.getParameter("action").equals(AdminConstants.NONE))) {
				action = request.getParameter("action");
			}
			if ((request.getParameter("userId") != null) && (!request.getParameter("userId").equals(AdminConstants.NONE))) {
				user = UserServiceDelegator.getUser(Integer.parseInt(request.getParameter("userId")));
				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("userId", new Object[]{user, SearchListCriteria.EQ});
				searchListCriteria.setAttribute("itemtype.id", new Object[]{4, SearchListCriteria.NE});
				itemsListTemp = ItemsServiceDelegator.getListBySearchListCriteria(searchListCriteria);
				for (Iterator<Items> iterator = itemsListTemp.iterator(); iterator.hasNext();) {
					Items type = (Items) iterator.next();
					searchListCriteria.removeAllAttribute();
					searchListCriteria.setAttribute(AttributeConstants.ITEMS, new Object[]{type, SearchListCriteria.EQ});
					searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
					itemsWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
					if(itemsWatchList != null){
						type.setNoOfWatches(itemsWatchList.size());
					}
					if(type.getEndTime() != null){
						if(type.getEndTime().before(DateUtil.getTodaysDate())){
							type.setClosed(true);
						}else{
							type.setClosed(false);
						}
					}
					itemsList.add(type);
				}
			}
		}catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}
	public String viewBuyerItem() throws Exception{
		if (request.getParameter("cid") != null) {
			searchListCriteria.removeAllAttribute();
			int itemId = Integer.parseInt(request.getParameter("cid"));
			searchListCriteria.setAttribute("items.itemId", new Object[]{itemId, SearchListCriteria.EQ});
			wonlist = AuctionServiceDelegator.getAuctionWonlistListBySearchListCriteria(searchListCriteria);
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("userId.id", wonlist.get(0).getUser().getId());
			searchCriteria.setAttribute("itemtype.id", 4);
			List<Items> listlist = ItemsServiceDelegator.getList(searchCriteria);
			for(int i=0;i<listlist.size();i++){
				if(listlist.get(i).getItemtype().getId() == 4){
					itemsList.add(listlist.get(i));
				}
			}
		}
		return SUCCESS;
	}

	public void getMyOffersWanted(){
		user = SessionUtil.getUser(request);
		listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		UserFavoritesUsers userFavoritesUsers = new UserFavoritesUsers();
		if(request.getParameter("cid") != null) {
			if(request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("addtofavorite") ) {
				askSellerId = Integer.parseInt(request.getParameter("cid"));
				itemAddTOFavorite = ItemsServiceDelegator.getItemsById(askSellerId);
				favoritesUsers.setUser(user);
				favoritesUsers.setUserIdFav(itemAddTOFavorite.getUserId());
				CommonFunctionDelegator.generalInsertbyobj(favoritesUsers);
			}
		}
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("user", user);
		searchCriteria.setAttribute("status.id", 4);

		itemOffersTemp = (List<ItemOffers>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), searchCriteria);

		List<ItemOffers> offerTenderList = new ArrayList<ItemOffers>();

		for (int i = 0; i < itemOffersTemp.size(); i++) {
			if(itemOffersTemp.get(i).getItems().getItemtype().getId() == 7){
				offerTenderList.add(itemOffersTemp.get(i));
			}
		}


		for (Iterator<ItemOffers> iterator = offerTenderList.iterator(); iterator.hasNext();) {
			ItemOffers type = (ItemOffers) iterator.next();
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("user", user);
			searchCriteria.setAttribute("userIdFav", type.getItems().getUserId());
			List<UserFavoritesUsers> tmpList = (List<UserFavoritesUsers>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserFavoritesUsers.class);
			userFavoritesUsers = (UserFavoritesUsers) (tmpList.size() > 0 ? tmpList.get(AdminConstants.ZERO_NUMBER) : null);
			type.getItems().setFavoritesUsers(userFavoritesUsers);
			Date offerEndTime = DateUtil.addDate(type.getTime(), Calendar.DATE, type.getDuration());
			type.setOfferEndTime(offerEndTime);
			if(offerEndTime.before(DateUtil.getTodaysDate())){
				type.setIsClosed(true);
			}else{
				type.setIsClosed(false);
			}
			itemOffers.add(type);
		}
	}

	public String myOffersWanted() throws Exception{
		getMyOffersWanted();
		return SUCCESS;
	}

	public String myWantedPurchased() throws Exception{
		user = SessionUtil.getUser(request);
		listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		searchListCriteria.removeAllAttribute();
		searchListCriteria.setAttribute("user", new Object[]{user, SearchListCriteria.NE});
		try{
			List<AuctionWonlist> list = AuctionServiceDelegator.getAuctionWonlistListBySearchListCriteria(searchListCriteria);
			for (int i=0;i<list.size();i++) {
				if((list.get(i).getItems().getItemtype().getId() == 7)){
					int id = list.get(i).getItems().getUserId().getId();
					if(id == user.getId()){
						wonlist.add(list.get(i));
					}
				}
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while getting list for My wanted Purchased items");
			return INPUT;
		}
	}

	public String myWantedSold() throws Exception{
		user = SessionUtil.getUser(request);
		listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("user", user);
		try{
			List<AuctionWonlist> list = (List<AuctionWonlist>) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), searchCriteria);
			for (int i=0;i<list.size();i++) {
				if(list.get(i).getItems().getItemtype().getId() == 7){
					if(list.get(i).getItems().getEndTime().before(DateUtil.getTodaysDate())){
						list.get(i).getItems().setClosed(true);
					}else{
						list.get(i).getItems().setClosed(false);
					}
					wonlist.add(list.get(i));
				}
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while getting list for My wanted Sold items");
			return INPUT;
		}
	}

	public String sendItemInvoice() throws Exception{
		user = SessionUtil.getUser(request);
		if(request.getParameter("cid") != null){
			int itemId = Integer.parseInt(request.getParameter("cid"));
			List<Criterion> criterionList = new ArrayList<Criterion>();
			criterionList.add(Restrictions.eq("id.itemId", itemId));
			criterionList.add(Restrictions.ne("user.id", user.getId()));
			transactionsList = (List<Transactions>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Transactions.class);
			for(int i=0;i<transactionsList.size();i++){
				transactionsMap.put(transactionsList.get(i).getUser().getId(), transactionsList.get(i));
			}
		}
		return SUCCESS;
	}

	public String deleteWatchingOthers() throws Exception{
		user = SessionUtil.getUser(request);
		if(request.getParameter("cid") != null){
			try{
				int itemId = Integer.parseInt(request.getParameter("cid"));
				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("user.id", new Object[]{user.getId() , SearchListCriteria.EQ,});
				searchListCriteria.setAttribute("items.itemId", new Object[]{itemId , SearchListCriteria.EQ,});
				List<ItemsWatchlist> itemlist  = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				ItemsWatchlist itemsWatchlist = (itemlist.size() > 0 ? itemlist.get(0) : null);
				if(itemsWatchlist != null){
					CommonFunctionDelegator.GeneralDelete(itemsWatchlist);
					MyAccountCount myAccountCount = ((MyAccountCount) request.getSession().getAttribute(AttributeConstants.MYACCOUNTCOUNT));
					if(myAccountCount.getItemWantedWatchingOthers() > 0){
						myAccountCount.setItemWantedWatchingOthers(myAccountCount.getItemWantedWatchingOthers() - 1);
					}
					request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);

					addActionMessage("Delete item watching others");
				}
			}catch (Exception e) {
				e.printStackTrace();
				addActionError("Error while delete item watching others");
				return INPUT;
			}
		}
		return SUCCESS;
	}

	public String deleteMyOffers() throws Exception{
		if(request.getParameter("check_list") == null) {
			addActionError("Please select item for delete offer");
			getMyOffersWanted();
			return INPUT;
		}else{
			String[] checkedId = request.getParameterValues("check_list");
			for (String chkid : checkedId) {
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("id",Integer.parseInt(chkid));
				List<ItemOffers> list = (List<ItemOffers>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemOffers.class);
				ItemOffers itemOffers = list.size() > 0 ? list.get(0) : null;
				if(itemOffers != null){
					CommonFunctionDelegator.GeneralDelete(itemOffers);
					MyAccountCount myAccountCount = ((MyAccountCount) request.getSession().getAttribute(AttributeConstants.MYACCOUNTCOUNT));
					if(myAccountCount.getItemWantedMyOffers() > 0){
						myAccountCount.setItemWantedMyOffers(myAccountCount.getItemWantedMyOffers() - 1);
					}
					request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);

				}
			}
		}
		addActionMessage("My offers delete successfully");
		getMyOffersWanted();
		return SUCCESS;
	}

	public List<ItemsWatchlist> getItemsWatchList() {
		return itemsWatchList;
	}

	public void setItemsWatchList(List<ItemsWatchlist> itemsWatchList) {
		this.itemsWatchList = itemsWatchList;
	}

	public List<Items> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<Items> itemsList) {
		this.itemsList = itemsList;
	}

	public List<ItemOffers> getItemOffersTemp() {
		return itemOffersTemp;
	}

	public void setItemOffersTemp(List<ItemOffers> itemOffersTemp) {
		this.itemOffersTemp = itemOffersTemp;
	}

	public List<AuctionWonlist> getWonlist() {
		return wonlist;
	}

	public void setWonlist(List<AuctionWonlist> wonlist) {
		this.wonlist = wonlist;
	}

	public Map<Integer, Transactions> getTransactionsMap() {
		return transactionsMap;
	}

	public void setTransactionsMap(Map<Integer, Transactions> transactionsMap) {
		this.transactionsMap = transactionsMap;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getRedirectAction() {
		return redirectAction;
	}

	public void setRedirectAction(String redirectAction) {
		this.redirectAction = redirectAction;
	}
}
